public class test {
    public int strToInt(String str) {
        str = str.trim();
        if(!Character.isDigit(str.charAt(0)) && str.charAt(0)!='-' && str.charAt(0)!='+'){
            return 0;
        }
        int flg = 1;
        int index = 0;
        if(str.charAt(0) == '-'){
            flg = -1;
            index++;
        }else if(str.charAt(0) == '+'){
            index++;
        }
        long res = 0;
        while(Character.isDigit(str.charAt(index))){
            res = res * 10 + str.charAt(index)-'0';
            if(res > Integer.MAX_VALUE && flg == 1){
                return Integer.MAX_VALUE;
            }else if (res > Integer.MAX_VALUE && flg == -1) {
                return Integer.MIN_VALUE;
            }
            index++;
        }
        return flg==1?res:-res;
    }
}
